#!/bin/sh

SCHEMA="com.canonical.Unity.Panel"
OBJECT="systray-whitelist"
APP="SWT"

TRY_GET=$(gsettings get $SCHEMA $OBJECT 2>/dev/null || echo "_FALSE_")
if [ "$TRY_GET" != "_FALSE_" ] ; then
    LIST=$(gsettings get $SCHEMA $OBJECT 2>/dev/null)

    ALL_ENABLED=$(echo "$LIST" | grep "'all'" 2>/dev/null)
    if [ "$ALL_ENABLED" = "$LIST" ] ; then
        echo "All applications are whitelisted by Unity system-tray; skipping"
        exit 0
    fi

    APP_ENABLED=$(echo "$LIST" | grep "'$APP'" 2>/dev/null)
    if [ "$APP_ENABLED" = "$LIST" ] ; then
        echo "Application '$APP' is already whitelisted by Unity system-tray; skipping"
        exit 0
    fi

    NEW_LIST=$(echo "$LIST" | sed -s -e "s#'\]#', '$APP']#g")
    echo "New Unity system-tray whitelist: $NEW_LIST"

    gsettings set $SCHEMA $OBJECT "$NEW_LIST"
    echo "Please logout and login to enable the new whitelist"
else
    echo "Can not find canonical Unity system-tray whitelist settings"
fi

exit 0
